define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wechat/card/index',
                    add_url: 'wechat/card/add',
                    edit_url: 'wechat/card/edit',
                    del_url: 'wechat/card/del',
                    multi_url: 'wechat/card/multi',
                    cardinfo: 'wechat/card/cardinfo',
                    table: 'wechat_card',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'store_id',title:__('store_id')},
                        {field: 's_name',title:__('店铺名')},
                        //{field: 'mp_id', title: __('Mp_id')},
                        //{field: 'logo_url', title: __('Logo_url'), formatter: Table.api.formatter.image},
                        //{field: 'brand_name', title: __('Brand_name')},
                        {field: 'title', title: __('Title')},
                        //{field: 'sub_title', title: __('Sub_title')},
                        //{field: 'color', title: __('Color')},
                        //{field: 'notice', title: __('Notice')},
                        //{field: 'service_phone', title: __('Service_phone')},
                        //{field: 'description', title: __('Description')},
                        //{field: 'type_text', title: __('Type'), operate:false},
                        //{field: 'fixed_term', title: __('Fixed_term')},
                        //{field: 'fixed_begin_term', title: __('Fixed_begin_term')},
                        {field: 'quantity', title: __('Quantity')},
                        {field: 'pass_check',title:__('审核情况'),formatter:function(res){
                            return res==1? __('通过审核'): __('未审核');
                        }},
                        // {field: 'status', title:__('Status'),formatter: function(res){
                        //     switch(res)
                        //     {
                        //         case 'CARD_STATUS_NOT_VERIFY':
                        //             return  __('CARD_STATUS_NOT_VERIFY');
                        //             break;
                        //         case 'CARD_STATUS_VERIFY_FAIL':
                        //             return  __('CARD_STATUS_VERIFY_FAIL');
                        //             break;
                        //         case 'CARD_STATUS_VERIFY_OK':
                        //             return  __('CARD_STATUS_VERIFY_OK');
                        //             break;
                        //         case 'CARD_STATUS_DELETE':
                        //             return  __('CARD_STATUS_DELETE');
                        //             break;
                        //         case 'CARD_STATUS_DISPATCH':
                        //             return  __('CARD_STATUS_DISPATCH');
                        //             break;
                        //     }
                        // }},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime},
                        {field: 'visible', title: __('Visible'), formatter: function(res){
                            return res==1? __('Normal'): __('Hidden');
                        }},

                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                        {field: 'id', title: __('Operate'), table: table, buttons: [
                            {
                                name: 'read',
                                text: __('See'),
                                title: __('See'),
                                icon: 'fa fa-edit',
                                classname: 'btn btn-xs btn-success btn-dialog', url: 'wechat/card/edit'},
                            {
                                name: 'del',
                                text: __('Del'),
                                title: __('Del'),
                                icon: 'fa fa-trash',
                                classname: 'btn btn-xs btn-ajax btn-danger btn-delone',
                                url: 'wechat/card/del',
                                confirm: '确定要执行该操作吗?'},
                            {
                                name: 'add_group',
                                text:__('加入主题'),
                                title:__('加入主题'),
                                icon: 'fa fa-edit',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url:'wechat/card/group'
                            },{
                                name: 'qrcode',
                                text:__('二维码生成'),
                                title:__('二维码生成'),
                                icon: 'fa fa-edit',
                                classname: 'btn btn-xs btn-info btn-dialog',
                                url:'wechat/card/qrCode'
                            },{
                                name: 'cardinfo',
                                text:__('审核请求'),
                                title:__('审核请求'),
                                icon: 'fa fa-edit',
                                classname: 'btn btn-xs btn-info btn-ajax',
                                url:'wechat/card/cardinfo'
                            }
                        ], events: Table.api.events.operate, formatter: Table.api.formatter.buttons,operate:false}
                ]]
            });

            //setTimeout(function(){
            //   $('.add_group').on('click',function(){
            //       layer.open({
            //           type: 0,
            //           title:'加入主题',
            //           area: ['500px'],
            //           btn: ['取消'],
            //           content: '<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action=""><div class="form-group"><label for="c-card_ids" class="control-label col-xs-12 col-sm-2">主题名</label> <div class="col-xs-12 col-sm-8"><select><option>8888</option></select></div></div></form>'
            //       });
            //   });
            //},3000);


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            //获取底部元素
            var footer = document.getElementById('layer-footer');
            // 获取点击按钮对象
            var submitNode = document.getElementById('form-submit');
            //减掉多少钱的元素
            var reduce_cost = document.getElementById('c-reduce_cost');
            //满多少金额的元素
            var least_cost = document.getElementById('c-least_cost');
            //卡券的库存量
            var quantity = document.getElementById('c-quantity');
            //开始时间
            var begin_timestamp = document.getElementById('c-begin_timestamp');
            //结束时间
            var end_timestamp = document.getElementById('c-end_timestamp');
            //使用类型
            var use_type = document.getElementById('c-use_type');
            //副标题
            var sub_title = document.getElementById('c-sub_title');
            //卡券类型CASH
            var card_type = document.getElementById('c-card_type');
            //平台类型
            var platform_type = document.getElementById('c-platform_type');
            //红包金额
            var business_price = document.getElementById('c-business_price');
            var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/;
            // platform_type.onchange = function()
            // {
            //     //广告商
            //     if(this.value == 3){
            //         sub_title.value = '百米平台均可使用';
            //         sub_title.setAttribute("readonly", "readonly");
            //     }
            // }

            //选择改变时
            // use_type.onchange  = function(){
            //     if(this.value == 0){
            //         sub_title.value = '店铺内通用';
            //         sub_title.setAttribute("readonly", "readonly");
            //     }else if(this.value == 1){
            //         sub_title.value = '';
            //         sub_title.removeAttribute('readonly');
            //     }
            // }



            submitNode.onclick = function(event){
                var myalert = $('#myalert');
                var alert_text =  $('#alert-text');
                begin_date = (new Date(begin_timestamp.value)).getTime();
                end_date = (new Date(end_timestamp.value)).getTime();
                // 判断条件
                // if((reduce_cost.value > least_cost.value) || (!reduce_cost.value || !least_cost.value) || (!quantity.value || quantity.value<0) || (end_date > begin_date)){
                //     $('#myalert').show();
                //     return false;
                // }
                if(card_type.value == 'CASH') {
                    var reduce = reduce_cost.value;
                    var least = least_cost.value;
                    if (parseInt(reduce) > parseInt(least)) {
                        myalert.show();
                        alert_text.html('输入的减满金额有误');
                        return false;
                    }
                }
                if(!quantity.value || (quantity.value <= 0)) {
                    myalert.show();
                    alert_text.html('库存数量输入有误');
                    return false;
                }else if(quantity.value > 10000){
                    myalert.show();
                    alert_text.html('库存数不得大于10000');
                    return false;
                }else if((end_date < begin_date) || (!end_date || !begin_date)) {
                    myalert.show();
                    alert_text.html('时间输入有误');
                    return false;
                }else if(document.getElementById('c-business_price')){
                    if(!reg.test(business_price.value)) {
                        myalert.show();
                        alert_text.html('清输入正确的红包金额');
                        return false;
                    }
                }else{
                    myalert.hide();
                }
            }
        },
        edit: function () {
            Controller.api.bindevent();
        },
        group: function (){
            Controller.api.bindevent();
            $('form').click(function(e){
                if($('#c-group_id').val() == 0){
                    e.preventDefault()
                }
            });
        },
        qrCode: function (){
            Controller.api.bindevent();
        },
        cardinfo:function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
